<?php
/*
 * emarket - a website to create your own online-shop.  
 * Copyright (C) 2009  Frédéric Delorme
 *
 * This file is part of emarket.
 *
 * emarket is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * emarket is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with emarket.  If not, see <http://www.gnu.org/licenses/>.
 *
 * To contact me: frederic.delorme@gmail.com
 * to see some information on te project : http://code.google.col/p/os-emarket/
 *
 */
require_once("webapp/application/application.php");
require_once("framework/paginator.php");
require_once("webapp/helpers/cart.helpers.php");
/**
 * Home page for this little demonstration site.
 * the <em>homeManager.php</em> is the main handler for data displayed on the
 * <strong>home.tpl</strong>, corresponding to the "<strong>home</strong>" page.
 * @author frederic
 *
 */
class HomeManager extends Application{
	private static $_postPaginator;
	private static $_productPaginator;
	
	/**
	 * (non-PHPdoc)
	 * @see framework/WebApp#initialize()
	 */
	public function initialize(){
		parent::initialize();
		__debug(__CLASS__."::initialize() - start",__FILE__,__LINE__);
/*		$this->cart = CartHelpers::get();
		__template_set("cart",$this->cart);
		$this->addEntity('category',
			__query_entities(
				"category",
					$where = array(array('field'=>"parent_id",'comparator'=>"=",'value'=>"0")),
					$order = array(	array('field'=>"parent_id",'comparator'=>"ASC"),
						 			array('field'=>"sortorder",'comparator'=>"ASC")),
					$limit = "")
			);*/
		$this->addEntity(
			'product',
			__query_entities(
				"product",
					$where = "",
					$order = $order = array(	array('field'=>"availableat_date",'comparator'=>"ASC")),
					$limit = array("max"=>6)
				)
			);	
		$this->addEntity(
			'post',
			__query_entities(
				"post",
					$where = "",
					$order = array( array('field'=>"created_at",'comparator'=>"ASC")),
					$limit = ""
				)
			);
		__template_set('categories',self::$_entities['category']);
		__template_set('posts',self::$_entities['post']);
		__template_set('products',self::$_entities['product']);

		__debug(__CLASS__."::initialize() - end",__FILE__,__LINE__);
	}
		
	/**
	 * Retrieve data for this manager on the List Action.
	 * @param $type
	 * @param $id
	 * @return unknown_type
	 */
	public function lister($type,$id){
			if($type=="category" && $id!="1"){
				$this->setEntity('category',
					__query_entities(
						"category",
							$where = array(array('field'=>"category_path",'comparator'=>"like",'value'=>"0.")),
							$order = array(	array('field'=>"parent_id",'comparator'=>"ASC"),
								 			array('field'=>"sortorder",'comparator'=>"ASC")),
							$limit = "")
					);
				$this->setEntity(
					'product',
					__query_entities(
						"product",
							$where = array( array('field'=>"category_id",'comparator'=>"=",'value'=>$id)),
							$order = $order = array(	array('field'=>"availableat_date",'comparator'=>"ASC")),
							$limit = array("max"=>6)
						)
					);	
				$this->setEntity(
					'post',
					__query_entities(
						"post",
							$where = array( array('field'=>"category_id",'comparator'=>"=",'value'=>$id)),
							$order = array( array('field'=>"created_at",'comparator'=>"ASC")),
							$limit = ""
						)
					);
				
			}else{
				$this->initialize();
			}
			__template_set('categories',self::$_entities['category']);
			__template_set('posts',self::$_entities['post']);
			__template_set('products',self::$_entities['product']);
			
			return "home/main";
	}
	
	public function	show($type,$id){
					$this->initialize();
					$this->showEntity = __entity($type,$id);
			return "home/show";
	}
	
	public function render(){
		return "home/main";
	}
}
?>